package com.fg.cloud.factory.controller.factory;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.druid.util.StringUtils;
import com.fg.cloud.common.PageUtils;
import com.fg.cloud.common.StatusCode;
import com.fg.cloud.common.quantity.ResultType;
import com.fg.cloud.common.validate.LongUtils;
import com.fg.cloud.framework.controller.BaseController;
import com.fg.cloud.manager.entry.factory.FactoryPaymentEntity;
import com.fg.cloud.manager.service.factory.FactoryPaymentService;

/**
 * @author liushengchao
 * @Description: 支付方式 业务
 * @date 2017年6月15日下午6:00:02
 *
 */
@Controller
@RequestMapping(value = "/payment")
public class FactoryPaymentController extends BaseController {

	@Autowired
	private FactoryPaymentService paymentService;

	/**
	 * Description:厂商支付方式新增
	 * 
	 * @param request
	 * @param response
	 * @param paymentEntity
	 * @author shaoqiping
	 * @date 2017年8月15日下午3:24:43
	 */
	@RequestMapping(value = "/insert", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void insert(HttpServletRequest request, HttpServletResponse response, FactoryPaymentEntity paymentEntity) {
		try {
			if (paymentEntity == null || StringUtils.isEmpty(paymentEntity.getPaymentName())
					|| StringUtils.isEmpty(paymentEntity.getPaymentAccont())) {
				doResult(response, false, StatusCode.LACKVALUE, null, "支付方式信息输入有误");
				return;
			}
			paymentEntity.setFacId(getCurrentFactoryUser().getFacId());
			setCreateInfo(paymentEntity, request);
			setUpdateInfo(paymentEntity, request);
			int flag = paymentService.insert(paymentEntity);
			if (flag > 0) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.ADD_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.ADD_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:支付方式修改
	 * 
	 * @param request
	 * @param response
	 * @param paymentEntity
	 * @author shaoqiping
	 * @date 2017年8月15日下午3:31:15
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void update(HttpServletRequest request, HttpServletResponse response, FactoryPaymentEntity paymentEntity) {
		try {
			if (paymentEntity == null || paymentEntity.getId() == null) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			paymentEntity.setFacId(getCurrentFactoryUser().getFacId());
			setUpdateInfo(paymentEntity, request);
			int flag = paymentService.update(paymentEntity);
			if (flag > 0) {
				doResult(response, true, StatusCode.SAVESUCCESS, null, ResultType.SAVE_SUCCESS);
			} else {
				doResult(response, false, StatusCode.SAVEERROR, null, ResultType.SAVE_FAULT);
			}
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}

	/**
	 * Description:查询指定支付方式详情
	 * 
	 * @param request
	 * @param response
	 * @param id
	 * @author shaoqiping
	 * @date 2017年8月15日下午3:45:43
	 */
	@RequestMapping(value = "/find", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void select(HttpServletRequest request, HttpServletResponse response,Long id) {
		try {
			if (id == null || LongUtils.isEmpty(id)) {
				doResult(response, false, StatusCode.LACKVALUE, null, ResultType.CHOOSE_INFO);
				return;
			}
			FactoryPaymentEntity paymentEntity = paymentService.find(id,getCurrentFactoryUser().getFacId());
			if (paymentEntity == null) {
				doResult(response, false, StatusCode.FINDERROR, null, ResultType.FIND_FAULT);
				return;
			}
			doResult(response, true, StatusCode.FINDSUCCESS, paymentEntity, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:条件分页查询
	 * 
	 * @param request
	 * @param response
	 * @param page
	 * @param rows
	 * @param sort
	 * @param order
	 * @param filterText
	 * @author shaoqiping
	 * @date 2017年8月15日下午4:29:09
	 */
	@RequestMapping(value = "/findList", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void selectPage(HttpServletRequest request, HttpServletResponse response, Integer page, Integer rows,
			String sort, String order, String filterText) {
		try {
			// page默认为1 rows默认为10
			if (page == null || page <= 0) {
				page = 1;
			}
			if (rows == null || rows <= 0) {
				rows = 10;
			}
			PageUtils<FactoryPaymentEntity> pageinfo = new PageUtils<FactoryPaymentEntity>(page, rows, sort, order);
			Map<String, Object> condition = pageinfo.getCondition();
			condition.put("filterText", filterText);
			condition.put("facId", getCurrentFactoryUser().getFacId());
			pageinfo.setCondition(condition);

			paymentService.findList(pageinfo);
			doResult(response, true, StatusCode.FINDSUCCESS, pageinfo, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
	
	/**
	 * Description:经销商选择支付方式
	 * 
	 * @param request
	 * @param response
	 * @param paymentType
	 * @author shaoqiping
	 * @date 2017年8月15日下午5:48:36
	 */
	@RequestMapping(value = "/payments", method = { RequestMethod.GET, RequestMethod.POST })
	@ResponseBody
	public void selectByFacId(HttpServletRequest request, HttpServletResponse response) {
		try {
			List<FactoryPaymentEntity> payments = paymentService.findByFacId(getCurrentFactoryUser().getFacId(),
					2);
			doResult(response, true, StatusCode.FINDSUCCESS, payments, ResultType.FIND_SUCCESS);
		} catch (Exception e) {
			e.printStackTrace();
			doResult(response, false, StatusCode.EXCEPTIONERROR, null, e.getMessage());
		}
	}
}
